library(here)
library(dplyr)
library(readr)
library(ppmf)
library(sf)

# Tabulate PPMF file
tab_vtds = function(version="12") {
    out_file = here(paste0("data/PA/pa_vtd_das_", version, ".rds"))
    if (file.exists(out_file)) return(invisible(NULL))

    ppmf_file = here(paste0("data-raw/ppmf_", version, ".csv"))
    pa_ppmf = read_ppmf(state="PA", path=ppmf_file)
    pa_ppmf = add_geoid(pa_ppmf, level="block")
    pa_blocks = agg(pa_ppmf)

    baf_path = here("data-raw/pa_baf.zip")
    if (!file.exists(baf_path)) {
        download.file("https://www2.census.gov/geo/docs/maps-data/data/baf/BlockAssign_ST42_PA.zip",
                      baf_path)
    }
    pa_baf = read_csv(unz(baf_path, "BlockAssign_ST42_PA_VTD.txt"), col_types="ccc") %>%
        mutate(precinct = paste0(COUNTYFP, "-", DISTRICT)) %>%
        select(GEOID=BLOCKID, precinct)

    pa_vtd = pa_blocks %>%
        full_join(pa_baf, by="GEOID") %>%
        mutate(across(where(is.integer), ~ coalesce(., 0L)),
               across(where(is.numeric), ~ coalesce(., 0))) %>%
        group_by(precinct) %>%
        summarize(across(where(is.numeric), sum))
    write_rds(pa_vtd, out_file, compress="xz")
}

# tabulate for each DAS version
tab_vtds("12")
tab_vtds("04")
tab_vtds("19")

# download 2010 swapped data from the Census
out_file = here("data/PA/pa_vtd_das_orig.rds")
if (!file.exists(out_file)) {
    if (!requireNamespace("blockpop", quietly=TRUE)) {
        devtools::install_github("CoryMcCartan/blockpop")
    }
    library(blockpop)

    pa_blocks_truth = blockpop::download_2010_vars("PA")
    pa_vtd_truth = pa_blocks_truth %>%
        rename(GEOID=block) %>%
        full_join(pa_baf, by="GEOID") %>%
        mutate(across(where(is.integer), ~ coalesce(., 0L)),
               across(where(is.numeric), ~ coalesce(., 0))) %>%
        group_by(precinct) %>%
        summarize(across(where(is.numeric), sum))
    write_rds(pa_vtd_truth, out_file, compress="xz")
}
